﻿Public Class frmPermis

    Private szSQL As String
    Private rs As ADODB.Recordset
    Dim OLD_CAT As String

    Private Sub frmPermis_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        InitListePermis(0)
        InitComboUnite(TE_UCODE1)
        InitComboUnite(TE_UCODE2)
        InitComboUnite(TE_UCODE3)
        InitComboUnite(TE_UCODE4)
        InitComboUnite(TE_UCODE5)
        InitComboUnite(TE_UCOND1)
        InitComboUnite(TE_UCOND2)
        InitComboUnite(TE_UCOND3)
        InitComboUnite(TE_UCOND4)
        InitComboUnite(TE_UCOND5)
        InitComboUnite(TE_UROUTE1)
        InitComboUnite(TE_UROUTE2)
        InitComboUnite(TE_UROUTE3)
        ' utilisateur autorisé à modifier ?
        If gUserAcces < 9 Then
            BO_Enregistrer.Enabled = False
        Else
            BO_Enregistrer.Enabled = True
        End If

    End Sub

    Private Sub InitListePermis(ByVal ix As Integer)

        Dim nb As Integer = 0

        ' effacer la listview
        LV_PERMIS.Items.Clear()
  
        ' Lire la table FORMULES
        szSQL = "CALL spGet_PERMIS('')"
        GetRecordset(ctDSN_AEM, szSQL, rs)

        ' Load the data.
        Do While Not rs.EOF
            LV_PERMIS.Items.Add(rs.Fields("cat").Value)
            LV_PERMIS.Items(nb).SubItems.Add(rs.Fields("lib").Value)
            LV_PERMIS.Items(nb).SubItems.Add(rs.Fields("type_examen").Value)
            rs.MoveNext()
            nb = nb + 1
        Loop
        ' Close the recordset AND connection.
        rs.Close()

        If nb > 0 Then
            LB_NbLignes.Text = CStr(nb) & " lignes"
        End If
        ' restaurer la position du curseur avant le init
        setIndice(ix, LV_PERMIS)

    End Sub

    Private Sub InitComboUnite(ByVal combo As ComboBox)

        ' vider le combobox
        combo.Items.Clear()
        combo.Items.Add("Jour")
        combo.Items.Add("Mois")
        combo.SelectedIndex = 0

    End Sub

    Private Sub LV_Permis_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles LV_PERMIS.DoubleClick

        If LV_PERMIS.SelectedItems.Count > 0 Then
            PermistoScreen(LV_PERMIS.SelectedItems(0).Text)
            ' afficher les détails de la formule
            tb_details.Visible = True
            TE_CAT.Focus()
            ' mémoriser ancien identifiant pour maj
            OLD_CAT = TE_CAT.Text
        End If

    End Sub

    Private Sub LV_Permis_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles LV_PERMIS.KeyDown
        Select Case e.KeyCode
            ' touche Enter = DoubleClick
            Case Keys.Enter
                LV_Permis_DoubleClick(sender, e)

            Case Keys.Insert
                BO_ADD_LIGNE_Click(sender, e)

            Case Keys.Delete
                BO_DELETE_LIGNE_Click(sender, e)

        End Select

    End Sub

    Private Sub BO_Enregistrer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_Enregistrer.Click

        ' mise à jour 
        szSQL = "CALL spUpd_PERMIS('" & OLD_CAT & "','" & TE_CAT.Text & "','" & CB_TYPE_EXAMEN.SelectedItem & "','" & _
            TE_LIB.Text & "'," & TE_ENCOURS.Value & "," & TE_HCODE.Value & "," & TE_AGECODE.Value & "," & _
            TE_DCODE1.Value & ",'" & TE_UCODE1.Text & "'," & TE_DCODE2.Value & ",'" & TE_UCODE2.Text & "'," & _
            TE_DCODE3.Value & ",'" & TE_UCODE3.Text & "'," & TE_DCODE4.Value & ",'" & TE_UCODE4.Text & "'," & _
            TE_DCODE5.Value & ",'" & TE_UCODE5.Text & "'," & TE_VCODE.Value & "," & TE_HEVAL.Value & "," & _
            TE_HCOND.Value & "," & TE_AGECOND.Value & "," & TE_NECHECP.Value & "," & _
            TE_DCOND1.Value & ",'" & TE_UCOND1.Text & "'," & TE_DCOND2.Value & ",'" & TE_UCOND2.Text & "'," & _
            TE_DCOND3.Value & ",'" & TE_UCOND3.Text & "'," & TE_DCOND4.Value & ",'" & TE_UCOND4.Text & "'," & _
            TE_DCOND5.Value & ",'" & TE_UCOND5.Text & "'," & TE_DROUTE1.Value & ",'" & TE_UROUTE1.Text & "'," & _
            TE_DROUTE2.Value & ",'" & TE_UROUTE2.Text & "'," & TE_DROUTE3.Value & ",'" & TE_UROUTE3.Text & "')"
        ExecQuery(ctDSN_AEM, szSQL)
        ' Cacher les détails
        BO_ANNULER_Click(sender, e)

    End Sub

    Private Sub PermistoScreen(ByVal cat As String)

        ' Lire l'enreg sélectionné
        szSQL = "CALL spGet_PERMIS('" & cat & "')"
        GetRecordset(ctDSN_AEM, szSQL, rs)
        If rs.RecordCount > 0 Then
            TE_CAT.Text = rs.Fields("cat").Value
            TE_LIB.Text = rs.Fields("lib").Value
            CB_TYPE_EXAMEN.SelectedIndex = CB_TYPE_EXAMEN.FindString(rs.Fields("type_examen").Value)
            TE_ENCOURS.Text = rs.Fields("EnCours").Value
            TE_VCODE.Text = rs.Fields("vcode").Value
            TE_HCODE.Text = rs.Fields("hcode").Value
            TE_HCOND.Text = rs.Fields("hcond").Value
            TE_HEVAL.Text = rs.Fields("heval").Value
            TE_AGECODE.Text = rs.Fields("agecode").Value
            TE_AGECOND.Text = rs.Fields("agecond").Value
            TE_NECHECP.Text = rs.Fields("nechecp").Value
            TE_DCODE1.Text = rs.Fields("DCODE1").Value
            TE_DCODE2.Text = rs.Fields("DCODE2").Value
            TE_DCODE3.Text = rs.Fields("DCODE3").Value
            TE_DCODE4.Text = rs.Fields("DCODE4").Value
            TE_DCODE5.Text = rs.Fields("DCODE5").Value
            TE_DCOND1.Text = rs.Fields("DCOND1").Value
            TE_DCOND2.Text = rs.Fields("DCOND2").Value
            TE_DCOND3.Text = rs.Fields("DCOND3").Value
            TE_DCOND4.Text = rs.Fields("DCOND4").Value
            TE_DCOND5.Text = rs.Fields("DCOND5").Value
            TE_DROUTE1.Text = rs.Fields("DROUTE1").Value
            TE_DROUTE2.Text = rs.Fields("DROUTE2").Value
            TE_DROUTE3.Text = rs.Fields("DROUTE3").Value
            TE_UCODE1.SelectedIndex = SetIndexCB(rs.Fields("UCODE1").Value)
            TE_UCODE2.SelectedIndex = SetIndexCB(rs.Fields("UCODE2").Value)
            TE_UCODE3.SelectedIndex = SetIndexCB(rs.Fields("UCODE3").Value)
            TE_UCODE4.SelectedIndex = SetIndexCB(rs.Fields("UCODE4").Value)
            TE_UCODE5.SelectedIndex = SetIndexCB(rs.Fields("UCODE5").Value)
            TE_UCOND1.SelectedIndex = SetIndexCB(rs.Fields("UCOND1").Value)
            TE_UCOND2.SelectedIndex = SetIndexCB(rs.Fields("UCOND2").Value)
            TE_UCOND3.SelectedIndex = SetIndexCB(rs.Fields("UCOND3").Value)
            TE_UCOND4.SelectedIndex = SetIndexCB(rs.Fields("UCOND4").Value)
            TE_UCOND5.SelectedIndex = SetIndexCB(rs.Fields("UCOND5").Value)
            TE_UROUTE1.SelectedIndex = SetIndexCB(rs.Fields("UROUTE1").Value)
            TE_UROUTE2.SelectedIndex = SetIndexCB(rs.Fields("UROUTE2").Value)
            TE_UROUTE3.SelectedIndex = SetIndexCB(rs.Fields("UROUTE3").Value)
        End If
        rs.Close()

    End Sub

    Private Function SetIndexCB(ByVal unite As String) As Integer
        If unite = "Mois" Then
            SetIndexCB = 1
        Else
            SetIndexCB = 0
        End If
    End Function

    Private Sub BO_ANNULER_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_ANNULER.Click
        ' Cacher les détails
        tb_details.Visible = False
        ' réaffiche la viewlist
        InitListePermis(LV_PERMIS.SelectedItems(0).Index)
    End Sub

    Private Sub BO_DELETE_LIGNE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_DELETE_LIGNE.Click
        ' utilisateur autorisé à supprimer ?
        If gUserAcces < 9 Then
            Exit Sub
        End If
        deleteEnregistrement("PERMIS", LV_PERMIS.SelectedItems(0).SubItems(1).Text, LV_PERMIS.SelectedItems(0).Text)
        ' Cacher les détails
        BO_ANNULER_Click(sender, e)
    End Sub

    Private Sub BO_ADD_LIGNE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BO_ADD_LIGNE.Click
        ' utilisateur autorisé à ajouter ?
        If gUserAcces < 9 Then
            Exit Sub
        End If
        ' Ajouter une nouvelle catégorie de permis
        szSQL = "CALL spINS_PERMIS('00000')"
        If ExecQuery(ctDSN_AEM, szSQL) Then
            ' réaffiche la viewlist puis sélectionne l'élément créé
            InitListePermis(0)
            ' ouvrir le fomulaire de modif
            LV_Permis_DoubleClick(sender, e)
        End If
    End Sub

End Class